package com.starhub.api.docwriting.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starhub.api.docwriting.dto.WritingRecordDTO;
import com.starhub.api.docwriting.result.WritingRecordDealResult;
import com.starhub.api.docwriting.service.WritingRecordService;
import com.starhub.api.docwriting.vo.WritingRecordVO;
import com.starhub.common.bean.ResultResp;
import com.starhub.common.security.util.SecurityUtils;
import com.starhub.common.sys.user.LoginUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**   
 * 写作记录管理API接口层
 * @version: V1.0
 * @author: liuhf
 * @time 2025-05-18
 */
@Api(tags = "写作记录管理")
@Slf4j
@RestController
@RequestMapping("/api/starhub/writing")
public class WritingRecordController {

    @Autowired
    private WritingRecordService writingRecordService;

    /**
     * 获取用户写作记录列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("获取用户写作记录列表")
    @GetMapping("/list")
    public ResultResp<Page<WritingRecordVO>> getWritingRecords(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            return ResultResp.success(writingRecordService.getWritingRecordsByUserId(pageNum, pageSize));
        } catch (Exception e) {
            log.error("获取用户写作记录列表异常 {}", e);
            return ResultResp.fail("获取用户写作记录列表异常：" + e.getMessage() + "," + e.getCause().getMessage(),e);
        }
    }

    /**
     * 获取写作记录详情
     * @param id
     * @return
     */
    @ApiOperation("获取写作记录详情")
    @GetMapping("/info/{id}")
    public ResultResp<WritingRecordDealResult> getWritingRecord(@PathVariable Long id) {
        try {
            return ResultResp.success(writingRecordService.getWritingRecordById(id));
        } catch (Exception e) {
            log.error("获取写作记录详情异常 {}", id, e);
            return ResultResp.fail("获取写作记录详情异常：" + e.getMessage() + "," + e.getCause().getMessage(),e);
        }
    }

    /**
     * 新增写作记录
     * @param dto
     * @return
     */
    @ApiOperation("新增写作记录")
    @PostMapping
    public ResultResp<WritingRecordDealResult> createWritingRecord(@RequestBody WritingRecordDTO dto) {
        LoginUser user = SecurityUtils.getCurrentUser();
        try {
            return ResultResp.success(writingRecordService.createWritingRecord(dto));
        } catch (Exception e) {
            log.error("新建异常 {}", user.getUserId(),e);
            return ResultResp.fail("新建异常：" + e.getMessage() + "," + e.getCause().getMessage(),e);
        }
    }

    /**
     * 编辑写作记录
     * @param dto
     * @return
     */
    @ApiOperation("编辑写作记录")
    @PutMapping
    public ResultResp<WritingRecordDealResult> updateWritingRecord(@RequestBody WritingRecordDTO dto) {
        try {
            return ResultResp.success(writingRecordService.updateWritingRecord(dto));
        } catch (Exception e) {
            log.error("编辑异常 {}", dto.getId(), e);
            return ResultResp.fail("编辑异常：" + e.getMessage() + "," + e.getCause().getMessage(),e);
        }
    }

    /**
     * 删除写作记录
     * @param id
     * @return
     */
    @ApiOperation("删除写作记录")
    @DeleteMapping("/{id}")
    public ResultResp<WritingRecordDealResult> deleteWritingRecord(@PathVariable Long id) {
        try {
            return ResultResp.success(writingRecordService.deleteWritingRecord(id));
        } catch (Exception e) {
            log.error("删除异常 {}", id, e);
            return ResultResp.fail("删除异常：" + e.getMessage() + "," + e.getCause().getMessage());
        }
    }

    @ApiOperation("添加到知识库")
    @PostMapping("/addToArchive/{id}")
    public ResultResp<WritingRecordDealResult> addToArchive(@PathVariable Long id) {
        try {
            return ResultResp.success(writingRecordService.addToArchive(id));
        } catch (Exception e) {
            log.error("保存为系统知识库异常 {}", id, e);
            return ResultResp.fail("保存为系统知识库异常：" + e.getMessage());
        }
    }
} 